package com.xbongbong.saas.script.tempenum.product;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.DefaultTypeEnum;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FormulaPoJo;
import com.xbongbong.paas.field.LinkedAttrPoJo;
import com.xbongbong.paas.field.NumericalLimitsPoJo;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.AmountFlagEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.SummaryTypeEnum;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.field.LinkKeyFieldInfoPojo;
import com.xbongbong.saas.enums.ForbiddenSettingEnum;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.SalesOpportunityEnum;
import com.xbongbong.saas.enums.product.OpportunityProductEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/10/15 15:25
 * @since v1.0
 */
@Getter
public enum TempOpportunityProductEnum {
    PRODUCT(OpportunityProductEnum.PRODUCT),
    UNIT(OpportunityProductEnum.UNIT),
    NUM(OpportunityProductEnum.NUM),
    PRICE(OpportunityProductEnum.PRICE),
    DISCOUNT(OpportunityProductEnum.DISCOUNT),
    SELLING_PRICE(OpportunityProductEnum.SELLING_PRICE),
    SELL_TOTAL(OpportunityProductEnum.SELL_TOTAL),
    MEMO(OpportunityProductEnum.MEMO),

    ;

    private OpportunityProductEnum opportunityProductEnum;

    TempOpportunityProductEnum(OpportunityProductEnum opportunityProductEnum) {
        this.opportunityProductEnum = opportunityProductEnum;
    }

    public static void initProduct(FieldAttrEntity fieldAttrEntity, List<LinkedAttrPoJo> linkedAttrPoJoList,List<LinkedAttrPoJo> linkedShowPoJoList, PaasFormEntityExt paasFormEntity){
        LinkInfoPojo linkInfoPojo = new LinkInfoPojo();
        linkInfoPojo.setLinkBusinessType(XbbRefTypeEnum.PRODUCT.getCode());
        linkInfoPojo.setLinkedAttr(linkedAttrPoJoList);
        linkInfoPojo.setLinkSaasMark(SaasMarkEnum.SAAS.getCode());
        linkInfoPojo.setLinkKey(linkedAttrPoJoList.get(1).getAttr());
        linkInfoPojo.setLinkFormId(paasFormEntity.getId());
        linkInfoPojo.setLinkMenuId(paasFormEntity.getMenuId());
        linkInfoPojo.setLinkAppId(paasFormEntity.getAppId());
        linkInfoPojo.setCondition(new ArrayList<>());
        LinkKeyFieldInfoPojo linkKeyFieldInfoPojo = new LinkKeyFieldInfoPojo();
        BeanUtil.copyProperties(linkedAttrPoJoList.get(1),linkKeyFieldInfoPojo);
        linkInfoPojo.setLinkKeyAttr(linkKeyFieldInfoPojo);
        linkInfoPojo.setLinkedShowAttr(linkedShowPoJoList);
        fieldAttrEntity.setLinkInfo(linkInfoPojo);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_VISIBLE_PERMISSION.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
        fieldAttrEntity.setShowEditable(1);
    }

    // 初始化产品中的其他属性
    public static void initPrice(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        fieldAttrEntity.setForbiddenSettingList(Arrays.asList(ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias(),ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias()));
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
    }

    public static void initDiscount(FieldAttrEntity fieldAttrEntity){
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "IF(OR(ISEMPTY({self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.PRICE.getAttr() + "}),ISEMPTY({self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SELLING_PRICE.getAttr() + "})," + "{self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SELLING_PRICE.getAttr() + "}==0" + ",{self."+ SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.PRICE.getAttr() +"}==0),100" +
                ",MULTIPLY(DIVIDE({self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SELLING_PRICE.getAttr() + "},{self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.PRICE.getAttr() + "}),100,2))";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.PRICE.getAttr(), "关联产品.单价");
        labelMap.put("self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SELLING_PRICE.getAttr(), "关联产品.售价（元）");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias(), ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
        fieldAttrEntity.setShowEditable(1);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
    }

    public static void initSellingPrice(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setIsForbidden(1);
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "DIVIDE(MULTIPLY({self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.PRICE.getAttr() + "},{self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.DISCOUNT.getAttr() + "}),100,6)";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.PRICE.getAttr(), "关联产品.单价");
        labelMap.put("self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.DISCOUNT.getAttr(), "关联产品.折扣（%）");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        // 合同金额默认限定 0 ~ 100000000000
        NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
        numericalLimits.setMin(0D);
        numericalLimits.setMax(100000000000D);
        fieldAttrEntity.setNumericalLimits(numericalLimits);
        fieldAttrEntity.setNumericalLimitsFlag(1);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        fieldAttrEntity.setShowEditable(1);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias(), ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    public static void initNum(FieldAttrEntity fieldAttrEntity){
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultValue(1);
        defaultAttrPoJo.setDefaultType(DefaultTypeEnum.CUSTOM.getAlias());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setIsForbidden(1);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        fieldAttrEntity.setShowEditable(1);
        fieldAttrEntity.setForbiddenSettingList(Collections.singletonList(ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias()));
    }

    public static void initSubTotal(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "{self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SELLING_PRICE.getAttr() + "}*{self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.NUM.getAttr() + "}";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SELLING_PRICE.getAttr(), "关联产品.售价");
        labelMap.put("self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.NUM.getAttr(), "关联产品.数量");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        // 合同金额默认限定 0 ~ 100000000000
        NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
        numericalLimits.setMin(0D);
        numericalLimits.setMax(100000000000D);
        fieldAttrEntity.setNumericalLimits(numericalLimits);
        fieldAttrEntity.setNumericalLimitsFlag(1);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias(), ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    public static void initUnit(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_ITEMS.getAlias(),
                ForbiddenSettingEnum.DISABLE_RULE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    /**
     * 预计金额
     * @param fieldAttrEntity
     */
    public static void initEstimateAmount(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "SUM({self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr() + "})";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr(), "关联产品.售价小计");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        // 合同金额默认限定 0 ~ 100000000000
        NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
        numericalLimits.setMin(0D);
        numericalLimits.setMax(100000000000D);
        fieldAttrEntity.setNumericalLimits(numericalLimits);
        fieldAttrEntity.setNumericalLimitsFlag(1);
    }

    public static void initMemo(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setIsForbidden(1);
        fieldAttrEntity.setShowEditable(1);
    }

}
